package com.swt.gap.controller;

import com.swt.gap.model.vo.EventNodeRelMediaVO;
import com.swt.gap.model.vo.FilterVO;
import com.swt.gap.model.vo.NodeFilterVO;
import com.swt.gap.model.vo.OntologyVO;
import com.swt.gap.service.NodeFilterService;
import com.swt.gap.util.UserLoginUtils;
import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 请填写说明
 *
 * @author sx
 * @date 2020/6/23 14:11
 */
@Api(tags = "过滤规则")
@RestController
@RequestMapping("/node/filter")
public class NodeFilterController {

    private final NodeFilterService nodeFilterService;

    public NodeFilterController(NodeFilterService nodeFilterService) {
        this.nodeFilterService = nodeFilterService;
    }

    @PostMapping
    @ApiOperation(value = "新增过滤规则", notes = "新增过滤规则")
    @ApiResponses({@ApiResponse(code = 201, message = "created", response = NodeFilterVO.class)})
    public ResponseEntity<NodeFilterVO> addFilter(@RequestBody NodeFilterVO nodeFilter) {
        Integer userId = UserLoginUtils.getUserId();
        nodeFilter.setCreatedBy(userId);
        nodeFilter.setCreatedTime(new Date());
        nodeFilter.setDeleted(false);
        nodeFilterService.addNodeFilter(nodeFilter);
        return ResponseEntity.ok(nodeFilter);
    }

    @GetMapping("/{filterId}")
    @ApiOperation(value = "获取过滤规则", notes = "获取过滤规则")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = NodeFilterVO.class)})
    public ResponseEntity<NodeFilterVO> getFilter(@PathVariable Integer filterId) {
        return ResponseEntity.ok(nodeFilterService.getFilterById(filterId));
    }

    @PutMapping("/{filterId}")
    @ApiOperation(value = "修改过滤规则", notes = "修改过滤规则")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = NodeFilterVO.class)})
    public ResponseEntity<NodeFilterVO> updateFilter(@PathVariable Integer filterId, @RequestBody NodeFilterVO nodeFilter) {
        nodeFilter.setFilterId(filterId);
        Integer userId = UserLoginUtils.getUserId();
        nodeFilter.setUpdatedBy(userId);
        nodeFilter.setUpdatedTime(new Date());
        nodeFilterService.updateNodeFilter(nodeFilter);
        return ResponseEntity.ok(nodeFilter);
    }

    @DeleteMapping("/{filterId}")
    @ApiOperation(value = "删除过滤规则", notes = "删除过滤规则")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = NodeFilterVO.class)})
    public ResponseEntity<Integer> deleteFilter(@PathVariable Integer filterId) {
        Integer userId = UserLoginUtils.getUserId();
        NodeFilterVO nodeFilter = new NodeFilterVO();
        nodeFilter.setFilterId(filterId);
        nodeFilter.setUpdatedBy(userId);
        nodeFilter.setUpdatedTime(new Date());
        nodeFilter.setDeleted(true);
        return ResponseEntity.ok(nodeFilterService.updateNodeFilter(nodeFilter));
    }


    @GetMapping
    @ApiOperation(value = "获取过滤规则列表", notes = "获取过滤规则列表，如不填写taskId参数则获取登录用户所有过滤规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "taskId",
                    value = "任务id,非必填，如不填写则获取登录用户所有过滤规则", required = false),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = NodeFilterVO.class, responseContainer = "List")})
    public ResponseEntity<List<NodeFilterVO>>listFilter(@RequestParam(required = false) Integer taskId) {
        Integer userId = UserLoginUtils.getUserId();
        return ResponseEntity.ok(nodeFilterService.listAllFilterByUserIdAndTaskId(userId, taskId));
    }

    @PostMapping("/node")
    @ApiOperation(value = "过滤", notes = "过滤")
    @ApiResponses({@ApiResponse(code = 200, message = "ok", response = NodeFilterVO.class)})
    public ResponseEntity<List<EventNodeRelMediaVO>> filter(@RequestBody FilterVO record) {
        return ResponseEntity.ok(nodeFilterService.filter(record));
    }
}
